package com.zhangdi.mgsync.core.listener;

import com.mongodb.MongoChangeStreamException;
import com.mongodb.MongoCommandException;
import com.zhangdi.mgsync.core.cache.Cache;
import com.zhangdi.mgsync.core.enums.Exchanges;
import com.zhangdi.mgsync.core.rabbit.AdminTemplate;
import com.zhangdi.mgsync.core.thread.BatchSendTimeoutScheduleder;
import com.zhangdi.mgsync.core.thread.ThreadManagement;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2021.10.21 15:00
 */
@Component
public class SyncMessageListener extends BaseMessageListener implements SyncListener {
    @Resource(name = "onlineMongoTemplate")
    MongoTemplate onlineMongoTemplate;
    @Resource(name = "tenderMongoTemplate")
    MongoTemplate tenderMongoTemplate;
    @Resource(name = "standardMongoTemplate")
    MongoTemplate standardMongoTemplate;

    @Resource(name = "onlineAdminTemplate")
    AdminTemplate onlineAdminTemplate;
    @Resource(name = "tenderAdminTemplate")
    AdminTemplate tenderAdminTemplate;
    @Resource(name = "standardAdminTemplate")
    AdminTemplate standardAdminTemplate;

    ThreadManagement threadManagement;

    public SyncMessageListener(ThreadManagement threadManagement) {
        this.threadManagement = threadManagement;
    }

    private AdminTemplate adminTemplate(String exchange) {
        if(Exchanges.mgsync.name().equals(exchange)) {
            return onlineAdminTemplate;
        } else if(Exchanges.tender.name().equals(exchange)) {
            return tenderAdminTemplate;
        } else if(Exchanges.standard.name().equals(exchange)) {
            return standardAdminTemplate;
        }
        return null;
    }

    private MongoTemplate mongoTemplate(String exchange) {
        if(Exchanges.mgsync.name().equals(exchange)) {
            return onlineMongoTemplate;
        } else if(Exchanges.tender.name().equals(exchange)) {
            return tenderMongoTemplate;
        } else if(Exchanges.standard.name().equals(exchange)) {
            return standardMongoTemplate;
        }
        return null;
    }

    /**
     * 启动监听
     *
     * @throws MongoCommandException
     * @throws MongoChangeStreamException
     */
    @Override
    public void startup(String exchange, Collection<String> collections, int seconds) throws MongoCommandException, MongoChangeStreamException {
        for (String collectionName : collections) {
            threadManagement.submit(adminTemplate(exchange), mongoTemplate(exchange).getCollection(collectionName), getCacheManager(), seconds);
        }
    }

    @Override
    public void startup(String exchange, String collectionName, int seconds) {
        threadManagement.submit(adminTemplate(exchange), mongoTemplate(exchange).getCollection(collectionName), getCacheManager(), seconds);
    }

    @Override
    public void shutdown() {
        logger.info("shutdown...");
        try {
            threadManagement.shutdown();
            BatchSendTimeoutScheduleder.get().shutdown();
        } catch (Exception e) {
            logger.error("shutdown error", e);
        }
    }

    @Override
    public void shutdown(String queue) {
        logger.info("shutdown...");
        try {
            threadManagement.shutdown();
        } catch (Exception e) {
            logger.error("shutdown error", e);
        }
    }

    @Override
    public String checkKey(String key) {
        Cache<String, String> cache = getCacheManager();
        return cache.get(key);
    }

    @Override
    public String addCache(String key, String val) {
        Cache<String, String> cache = getCacheManager();
        cache.put(key, val);
        return cache.get(key);
    }

    /**
     * 全量更新
     */
//    @Override
//    public void startFullUpdate(long second) {
////        mgThreadPool.execute(new FullUpdateWorkerThread(syncApiMap, this, second));
////
////        Iterator<Map.Entry<String, SyncAdapter<MyDocument, Object>>> iterator = syncApiMap.entrySet().iterator();
////        while (iterator.hasNext()) {
////            SyncAdapter<MyDocument, Object> adapter = iterator.next().getValue();
////            mgThreadPool.execute(() -> adapter.fullUpdate());
////        }
//    }
}
